﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KCL.Code.Data
{
    public class ColorDAO : BaseDAO
    {
        public List<Color> GetAllColors()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var lstColors = from c in ctx.Colors
                                where c.isActive == true
                                orderby c.name
                                select c;
                return lstColors.AsEnumerable().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool Insert(Color c)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.Colors.InsertOnSubmit(c);
                ctx.SubmitChanges();
                return true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.Number == 2627)
                {
                    try
                    {
                        KCLDataContext ctx = new KCLDataContext();
                        var colorToRestore = (from cl in ctx.Colors
                                              where cl.name.ToUpper() == c.name.ToUpper()
                                              && cl.isActive == false
                                              select cl).FirstOrDefault();
                        colorToRestore.name = c.name;
                        colorToRestore.isActive = true;
                        ctx.SubmitChanges();
                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(Color c)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var colorToUpdate = (from cl in ctx.Colors
                                     where cl.id == c.id
                                     select cl).FirstOrDefault();
                colorToUpdate.name = c.name.Trim();
                colorToUpdate.isActive = c.isActive;
                if (colorToUpdate.name.Length == 0)
                    return false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(Color c)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var colorToDelete = (from cl in ctx.Colors
                                     where cl.id == c.id
                                     select cl).FirstOrDefault();
                colorToDelete.isActive = false;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}